package cn.edu.lingnan.servlet;

import cn.edu.lingnan.pojo.Competition;
import cn.edu.lingnan.service.ICompetitionService;
import cn.edu.lingnan.service.IStudentService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
@WebServlet("/admin/delCom")
public class DelCompetition extends HttpServlet{
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //1
            String cid = req.getParameter("cid");
            String flag = req.getParameter("flag");
            boolean success;
            System.out.println("[Debug 070] the flag is value is " + flag);
            System.out.println("[Debug 071] the cid is value is " + cid);
            //2
            ICompetitionService service = new ICompetitionService();

            if(flag!=null){//删除多个记录
                String[] allCid = cid.split(",");
                for (String oneCid:allCid
                ) {
                    boolean result = service.isCompeteRegPart(Integer.parseInt(oneCid));//返回true表示这个项目被选过，不能删除
                    if(result){//只要有一个项目被选过，将交给错误页面处理，不执行删除
                        resp.sendRedirect("/admin/DelComError.html");
                        return;
                    }
                }
                //第一循环排除所有的项目没有被选过，再进行第二循环删除所有被选过的项目
                for (String oneCid:allCid
                     ) {
                    success = service.delCompetitionById(Integer.parseInt(oneCid));//getParameter拿到的是字符类型，本方法参数为int
                    if(!success){
                        resp.sendRedirect("/admin/DelComError.html");
                    }
                }
                resp.sendRedirect("/queryAllCompete");
            }else {//否则删除单个记录
                success = service.delCompetitionById(Integer.parseInt(cid));//getParameter拿到的是字符类型，本方法参数为int
                if(success){
                    resp.sendRedirect("/queryAllCompete");
                }else {
                    resp.sendRedirect("/admin/DelComError.html");
                }
            }
            //3

        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            super.doPost(req, resp);
        }
}
